package com.miyu.system.utils.crawler.book;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSONObject;
import com.miyu.system.pojo.crawler.BookPageVo;
import com.miyu.system.utils.crawler.CrawlerUtils;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.QueueScheduler;
import us.codecraft.webmagic.scheduler.RedisScheduler;
import us.codecraft.webmagic.utils.HttpConstant;

import java.util.HashMap;
import java.util.Map;


/**
 * 书籍爬虫解析
 *
 * @author longm
 * @date 2024/11/21
 */
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class BookPage implements PageProcessor {

    static private JedisPool jedisPool;

    public static JSONObject bookSpider(BookPageVo bpv){
        Request request = new Request();
        request.setUrl(bpv.getUrl());
        if(bpv.getMethod().equals(HttpConstant.Method.POST)){
            request.setMethod(HttpConstant.Method.POST);
            if(bpv.getParamsType().equals(CrawlerUtils.JSON)){
                request.setRequestBody(HttpRequestBody.json(JSONObject.toJSONString(bpv.getParams()),"utf-8"));
            }else{
                request.setRequestBody(HttpRequestBody.form(bpv.getParams(),"utf-8"));
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("params",bpv);
        request.setExtras(map);
        Spider spider =new Spider(new BookPage());
        spider.addRequest(request);
        spider.thread(5);
//        spider.setScheduler(new RedisScheduler(jedisPool));
                //最后数据人缓存
//                .addPipeline(new SaveCacheData())





        spider.run();
        log.info("存在线程数：{}",spider.getThreadAlive());
        log.info("状态：{}",spider.getStatus());
        log.info("url：{}",spider.getPageCount());
        log.info("sc：{}",spider.getScheduler());

        return null;

    }


    /**
     * 书籍解析入口
     * @param page
     */
    @SneakyThrows
    @Override
    public void process(Page page) {
        /*参数*/
        BookPageVo bpv= page.getRequest().getExtra("params");
        String operate = bpv.getOperate();//当前操作
        switch (BookOperate.getTagByCode(operate)){
            case BOOK_SEARCH:
                CommonBookJx.bookSearch(page);
                 break;
            case BOOK_DETAIL:
                CommonBookJx.bookDetail(page);
                break;
            case BOOK_CHAPTER:
                CommonBookJx.bookChapter(page);
                break;
            case BOOK_READER:
                CommonBookJx.bookReader(page);
                break;
            case BOOK_TYPE:
                break;
            case DOWNLOAD:
                break;
            case COLLECT:
                CommonBookJx.collectBook(page);
                break;
            case CHECKRULE:
                break;
            default:
                break;
        }

    }

    /**
     * @return
     */
    @Override
    public Site getSite() {
        return this.site;
    }

    /** 爬虫配置信息设置 有条件动态配置 */
    private Site site = new Site()
            /*设置编码*/
            .setCharset("utf-8")
            /*设置抓取间隔*/
            .setSleepTime(1)
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (HTML, like Gecko) Chrome/113.0.0.0 Safari/537.36")
            /*设置超时时间*/
            .setTimeOut(10000)
            /* 设置重试时间*/
            .setRetrySleepTime(300)
            /*设置重试次数*/
            .setRetryTimes(3);
}

